package com.atguigu.distributed.lock.leecode.tree;

import com.atguigu.distributed.lock.leecode.TreeNode;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * 力扣中等107二叉树的层序遍历II
 */
public class LevelOrderTwo {
    List<List<Integer>> ans = new ArrayList<>();
    public List<List<Integer>> LevelOrderTwo(TreeNode root){
        if(root==null){
            return ans;
        }
        dfs(root,0);
        Collections.reverse(ans);
        return ans;
    }
    private void dfs(TreeNode root,int level){
        if(root==null){
            return;
        }
        if(ans.size()==level){
            ans.add(new ArrayList<Integer>());
        }
        List<Integer> integers = ans.get(level);
        integers.add(root.data);

        if(root.left!=null){
            dfs(root.left,level+1);
        }
        if(root.right!=null){
            dfs(root.right,level+1);
        }
    }
}
